﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using com.vs.DataAccess.DO;

using com.vs.General;
namespace com.vs.DataAccess.DO
{
    public partial class ItemUnitOfMeasureDO : BaseDO
    {

        #region "Get Model"
        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性INTERNAL_ITEM_UM也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public string Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "ITM_IUOM";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return "" + helper.Insert(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public void Delete(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "SP_ITEMUNIOFMEASURE_DeleteByPK";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, UpdateWherePara(this));
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public void Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "ITM_UUOM";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象ITEMUNIOFMEASURE的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<ItemUnitOfMeasureDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;;
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, SelectSequel);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }


        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性ITEMUNIOFMEASURE对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static List<ItemUnitOfMeasureDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                dtModel = helper.GetTable(CommandType.Text, stSQL, paras);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }


        /// <summary>
        /// 从DataView装载持久性ITEMUNIOFMEASURE对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<ItemUnitOfMeasureDO> LoadListFromDataView(DataView dv)
        {
            List<ItemUnitOfMeasureDO> list = new List<ItemUnitOfMeasureDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }


        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static ItemUnitOfMeasureDO GetModel(DataRow row)
        {
            ItemUnitOfMeasureDO itemUMDO = new ItemUnitOfMeasureDO();
            if (!row["INTERNAL_ITEM_UM"].Equals(DBNull.Value)) itemUMDO.INTERNAL_ITEM_UM = Convert.ToInt32(row["INTERNAL_ITEM_UM"]);
            if (!row["ITEM"].Equals(DBNull.Value)) itemUMDO.ITEM = Convert.ToString(row["ITEM"]);
            if (!row["COMPANY"].Equals(DBNull.Value)) itemUMDO.COMPANY = Convert.ToString(row["COMPANY"]);
            if (!row["SEQUENCE"].Equals(DBNull.Value)) itemUMDO.SEQUENCE = Convert.ToInt32(row["SEQUENCE"]);
            if (!row["QUANTITY_UM"].Equals(DBNull.Value)) itemUMDO.QUANTITY_UM = Convert.ToString(row["QUANTITY_UM"]);
            if (!row["CONVERSION_QTY"].Equals(DBNull.Value)) itemUMDO.CONVERSION_QTY = Convert.ToInt32(row["CONVERSION_QTY"]);
            if (!row["LENGTH"].Equals(DBNull.Value)) itemUMDO.LENGTH = Convert.ToDouble(row["LENGTH"]);
            if (!row["WIDTH"].Equals(DBNull.Value)) itemUMDO.WIDTH = Convert.ToDouble(row["WIDTH"]);
            if (!row["HEIGHT"].Equals(DBNull.Value)) itemUMDO.HEIGHT = Convert.ToDouble(row["HEIGHT"]);
            if (!row["DIMENSION_UM"].Equals(DBNull.Value)) itemUMDO.DIMENSION_UM = Convert.ToString(row["DIMENSION_UM"]);
            if (!row["WEIGHT"].Equals(DBNull.Value)) itemUMDO.WEIGHT = Convert.ToDouble(row["WEIGHT"]);
            if (!row["WEIGHT_UM"].Equals(DBNull.Value)) itemUMDO.WEIGHT_UM = Convert.ToString(row["WEIGHT_UM"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) itemUMDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["PROCESS_STAMP"].Equals(DBNull.Value)) itemUMDO.PROCESS_STAMP = Convert.ToString(row["PROCESS_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) itemUMDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["TREAT_FULL_PCT"].Equals(DBNull.Value)) itemUMDO.TREAT_FULL_PCT = Convert.ToDouble(row["TREAT_FULL_PCT"]);
            if (!row["MOVEMENT_CLS"].Equals(DBNull.Value)) itemUMDO.MOVEMENT_CLS = Convert.ToString(row["MOVEMENT_CLS"]);
            if (!row["ITEM_CLASS"].Equals(DBNull.Value)) itemUMDO.ITEM_CLASS = Convert.ToString(row["ITEM_CLASS"]);
            if (!row["TREAT_AS_LOOSE"].Equals(DBNull.Value)) itemUMDO.TREAT_AS_LOOSE = Convert.ToString(row["TREAT_AS_LOOSE"]);
            if (!row["GROUP_DURING_CHECKIN"].Equals(DBNull.Value)) itemUMDO.GROUP_DURING_CHECKIN = Convert.ToString(row["GROUP_DURING_CHECKIN"]);

            return itemUMDO;
        }
        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select * From [ITEM_UNIT_OF_MEASURE] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDataParameter[] ValueParas(ItemUnitOfMeasureDO itemUMDO)
        {
            SqlParameter[] paras = new SqlParameter[20];
            paras[0] = new SqlParameter("@INTERNAL_ITEM_UM", itemUMDO.INTERNAL_ITEM_UM);
            paras[1] = new SqlParameter("@ITEM", itemUMDO.ITEM == String.Empty ? DBNull.Value : (object)itemUMDO.ITEM);
            paras[2] = new SqlParameter("@COMPANY", itemUMDO.COMPANY == String.Empty ? DBNull.Value : (object)itemUMDO.COMPANY);
            paras[3] = new SqlParameter("@SEQUENCE", itemUMDO.SEQUENCE);
            paras[4] = new SqlParameter("@QUANTITY_UM", itemUMDO.QUANTITY_UM == String.Empty ? DBNull.Value : (object)itemUMDO.QUANTITY_UM);
            paras[5] = new SqlParameter("@CONVERSION_QTY", itemUMDO.CONVERSION_QTY);
            paras[6] = new SqlParameter("@LENGTH", itemUMDO.LENGTH);
            paras[7] = new SqlParameter("@WIDTH", itemUMDO.WIDTH);
            paras[8] = new SqlParameter("@HEIGHT", itemUMDO.HEIGHT);
            paras[9] = new SqlParameter("@DIMENSION_UM", itemUMDO.DIMENSION_UM == String.Empty ? DBNull.Value : (object)itemUMDO.DIMENSION_UM);
            paras[10] = new SqlParameter("@WEIGHT", itemUMDO.WEIGHT);
            paras[11] = new SqlParameter("@WEIGHT_UM", itemUMDO.WEIGHT_UM == String.Empty ? DBNull.Value : (object)itemUMDO.WEIGHT_UM);
            paras[12] = new SqlParameter("@USER_STAMP", itemUMDO.USER_STAMP == String.Empty ? DBNull.Value : (object)itemUMDO.USER_STAMP);
            paras[13] = new SqlParameter("@PROCESS_STAMP", itemUMDO.PROCESS_STAMP == String.Empty ? DBNull.Value : (object)itemUMDO.PROCESS_STAMP);
            paras[14] = new SqlParameter("@DATE_TIME_STAMP", itemUMDO.DATE_TIME_STAMP == DateTime.MinValue ? DBNull.Value : (object)itemUMDO.DATE_TIME_STAMP);
            paras[15] = new SqlParameter("@TREAT_FULL_PCT", itemUMDO.TREAT_FULL_PCT);
            paras[16] = new SqlParameter("@MOVEMENT_CLS", itemUMDO.MOVEMENT_CLS == String.Empty ? DBNull.Value : (object)itemUMDO.MOVEMENT_CLS);
            paras[17] = new SqlParameter("@ITEM_CLASS", itemUMDO.ITEM_CLASS == String.Empty ? DBNull.Value : (object)itemUMDO.ITEM_CLASS);
            paras[18] = new SqlParameter("@TREAT_AS_LOOSE", itemUMDO.TREAT_AS_LOOSE == String.Empty ? DBNull.Value : (object)itemUMDO.TREAT_AS_LOOSE);
            paras[19] = new SqlParameter("@GROUP_DURING_CHECKIN", itemUMDO.GROUP_DURING_CHECKIN == String.Empty ? DBNull.Value : (object)itemUMDO.GROUP_DURING_CHECKIN);

            paras[0].DbType = DbType.Decimal;
            paras[1].DbType = DbType.String;
            paras[2].DbType = DbType.String;
            paras[3].DbType = DbType.Decimal;
            paras[4].DbType = DbType.String;
            paras[5].DbType = DbType.Decimal;
            paras[6].DbType = DbType.Decimal;
            paras[7].DbType = DbType.Decimal;
            paras[8].DbType = DbType.Decimal;
            paras[9].DbType = DbType.String;
            paras[10].DbType = DbType.Decimal;
            paras[11].DbType = DbType.String;
            paras[12].DbType = DbType.String;
            paras[13].DbType = DbType.String;
            paras[14].DbType = DbType.DateTime;
            paras[15].DbType = DbType.Decimal;
            paras[16].DbType = DbType.String;
            paras[17].DbType = DbType.String;
            paras[19].DbType = DbType.String;

            return paras;
        }

        private static IDataParameter[] UpdateWherePara(ItemUnitOfMeasureDO itemUMDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@INTERNAL_ITEM_UM", itemUMDO.INTERNAL_ITEM_UM);
            return paras;
        }

        #endregion



        public static List<ItemUnitOfMeasureDO> GetListByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumns(sess, dcParam, stOrderBy);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static DataTable GetTableByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}

